package com.shushan.accessibility.smartisan;

import android.view.accessibility.AccessibilityNodeInfo;

import com.shushan.accessibility.AccessibilityOperator;

import java.util.List;

/**
 * @ClassName: SmartisanOperator
 * @Desciption: 锤子辅助功能操作类
 * @author: yichaohua
 * @date: 2018-08-08
 */
public class SmartisanOperator {

    private static final String TAG = "SmartisanOperator";

    private static SmartisanOperator mInstance = new SmartisanOperator();

    public static SmartisanOperator getInstance() {
        return mInstance;
    }

    public boolean closeSavePower() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        boolean result = AccessibilityOperator.getInstance().clickListItem("android:id/list","书山宝",1);
        if(!result){
            return false;
        }else {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            List<AccessibilityNodeInfo> list = AccessibilityOperator.getInstance().findNodesByText("取消后台常驻");
            if(list != null && list.size() > 0){
                return true;
            }else {
                return AccessibilityOperator.getInstance().clickByText("后台常驻");

            }

        }
    }

    public boolean openComplete() {
        boolean result = AccessibilityOperator.getInstance().clickById("com.smartisanos.security:id/receive_boot_completed");
        if(!result){
            return false;
        }else {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            result = AccessibilityOperator.getInstance().clickListItem("书山宝",1);
            if(!result){
                return false;
            }else {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                return AccessibilityOperator.getInstance().clickById("com.smartisanos.security:id/system_switcher");
            }
        }
    }

    public boolean openFloatManager() {
        boolean result = AccessibilityOperator.getInstance().clickScrollItem("桌面悬浮窗",1);
        if(!result){
            return false;
        }else {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            result = AccessibilityOperator.getInstance().clickListItem("书山宝",1);
            if(!result){
                return false;
            }else {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                return AccessibilityOperator.getInstance().clickByText("始终允许");
            }
        }
    }

    public boolean enableAdmin() {
        boolean result = AccessibilityOperator.getInstance().clickById("com.android.settings:id/action_button");
        if(!result){
            return false;
        }else {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
            return AccessibilityOperator.getInstance().clickById("android:id/button1");
        }
    }

    public boolean openUsageSetting() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        boolean result = AccessibilityOperator.getInstance().clickListSwitch("android:id/list","smartisanos.widget.SwitchEx","书山宝",1,true);
        if(!result){
            return false;
        }else {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return AccessibilityOperator.getInstance().clickById("android:id/button1");
        }
    }
}
